﻿
layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer;

    $(function () {
        getLoginUserInfo();
        loadUnReadNoticeList();
    });
});

//获取登录用户信息
function getLoginUserInfo() {
    app.get('Index.ashx', {
        ActionType: 'LoginUserInfo',
        rmd: Math.random()
    }, function (data) {
        if (data) {
            $('.spUserAccount').text(data.UserAccount);
            $('.spUserName').text(data.UserName);
            if (data.PortraitUrl) {
                $('.img-circle').attr('src', data.PortraitUrl + '?t=' + Math.random());
            } else {
                $('.img-circle').attr('src', '/resources/images/profile_small.jpg');
            }
        }
    });
}

//退出
function logout() {
    layer.confirm('确定退出系统吗?', { icon: 3, title: '提示' }, function (index) {
        app.ajax('/LoginHandle.ashx', {
            PostType: 'loginOut',
            rmd: Math.random()
        }, function (data) {
            if (data.success) {
                window.location.href = "/Login.aspx";

                //退出清除cookie
                var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
                if (keys) {
                    for (var i = keys.length; i--;) {
                        document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString(); //清除当前域名下的
                    }
                }
            }
        });
    });
}

function openChangeUserPwd() {
    layer.open({
        title: '修改密码',
        type: 1,
        area: ['480px', '300px'], //宽高
        content: $('#tbChangeUserPwdHtml').html(),
        success: function (layero, dIndex) {
            // 表单提交事件
            layui.form.on('submit(submitChangeUserPwd)', function (data) {
                var fieldData = data.field;

                if (fieldData.OldUserPwd == "") {
                    top.layer.msg("原密码不能为空!", { icon: 5 });
                    return false;
                }
                if (fieldData.NewUserPwd == "") {
                    top.layer.msg("新密码不能为空!", { icon: 5 });
                    return false;
                }
                if (fieldData.ConfirmNewUserPwd == "") {
                    top.layer.msg("确认新密码不能为空!", { icon: 5 });
                    return false;
                }
                if (fieldData.NewUserPwd != fieldData.ConfirmNewUserPwd) {
                    top.layer.msg("两次密码设置不一样!", { icon: 5 });
                    return false;
                }
                if (!IsComplexPwd(fieldData.NewUserPwd)) {
                    layer.msg("密码必须大于8位且要包含大小写字母、数字！", { icon: 5 });
                    return false;
                }

                var loadIndex = layer.load(2);
                app.set('Index.ashx', {
                    ActionType: 'ChangeUserPwd',
                    OldUserPwd: fieldData.OldUserPwd,
                    NewUserPwd: fieldData.NewUserPwd,
                    rmd: Math.random()
                }, function (data) {
                    layer.close(loadIndex);
                    if (data.success) {
                        layer.close(dIndex);
                        layer.msg(data.msg, { icon: 1 });
                    } else {
                        layer.msg(data.msg, { icon: 2 });
                    }
                });
                return false;
            });
        }
    });
}

function closeChangeUserPwd() {
    top.layer.closeAll();
}

//初始化未读消息列表
function loadUnReadNoticeList() {
    app.get('/Business/Notice/Notice.ashx', {
        ActionType: 'UnReadNoticeInfo'
    }, function (data) {
        $('#tbUnReadNoticeNum').text(data.total);
        if (data.total > 0) {
            $('#tbUnReadNoticeNum').show();
        } else {
            $('#tbUnReadNoticeNum').hide();
        }
        if (data.rows.length > 0) {
            $('#tbNoticeList').find("li:not(#tbShowAllUnReadNoticeList)").remove();
            var noticeHtml = '';
            $.each(data.rows, function (i, item) {
                noticeHtml += '<li class="m-t-xs">';
                noticeHtml += '<div class="dropdown-messages-box">';
                noticeHtml += '<div class="media-body">';
                if (item.IsRead == 0) {
                    noticeHtml += '<a style="padding: 0px;" onclick="showNoticeViewDetails(\'' + item.GUID + '\')"><strong>' + item.NoticeTitle + '</strong></a><br />';
                } else {
                    noticeHtml += '<a style="padding: 0px;" onclick="showNoticeViewDetails(\'' + item.GUID + '\')">' + item.NoticeTitle + '</a><br />';
                }
                noticeHtml += '<small class="text-muted">' + app.getTDate2(item.PublishDate) + '</small>';
                noticeHtml += '</div>';
                noticeHtml += '</div>';
                noticeHtml += '</li>';
                noticeHtml += '<li class="divider"></li>';
            });
            $('#tbShowAllUnReadNoticeList').before(noticeHtml);
        }
    });
}

function showNoticeViewDetails(id) {
    app.set('/Business/Notice/Notice.ashx', {
        ActionType: 'ReadNotice',
        GUID: id
    }, function (data) {
        if (data.success) {
            loadUnReadNoticeList();
        }
        layer.open({
            title: "查看通知",
            type: 2,
            area: ["900px", "600px"],
            scrollbar: false,
            content: "/Business/Notice/NoticeViewDetails.aspx?GUID=" + id
        });
    });
}
